//
//  LLVIPViewController.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLVIPViewController.h"
#import "LLVIPViewModel.h"
#import "LLBannerCollectionViewCell.h"

@interface LLVIPViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
@property(weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic, strong, readwrite) LLVIPViewModel *viewModel;
@end

@implementation LLVIPViewController
@dynamic viewModel;

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.viewModel = [[LLVIPViewModel alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.edgesForExtendedLayout = UIRectEdgeBottom;

    [self.collectionView registerClass:[LLBannerCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_BANNER];
}

- (void)bindViewModel {
    [super bindViewModel];

    @weakify(self)
    [RACObserve(self.viewModel, dataSource)
            subscribeNext:^(id _) {
                @strongify(self)
                [self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]];
//         [self.collectionView reloadSections:<#(NSIndexSet *)sections#>];
            }];

    [[RACObserve(self.viewModel, isLoading)
            distinctUntilChanged]
            subscribeNext:^(NSNumber *isLoading) {
                @strongify(self)
                if ([isLoading boolValue]) {
                    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES].labelText = @"Logging in...";
                } else {
                    [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
                }
            }];

    [self.viewModel.loadMenusCommand execute:nil];
}

#pragma mark - DataSource
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    if ([self.viewModel.dataSource[section] count] == 0) {
        return 0;
    }
    if (section == 0 && self.viewModel.bannerDataSource.count > 0) {
        return 1;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LLBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_BANNER
                                                                                     forIndexPath:indexPath];
        NSObject *viewModel = self.viewModel.bannerDataSource[indexPath.item];
        [cell bindViewModel:viewModel];
        return cell;
    }
    return nil;
}

#pragma mark - Delegate
#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(FULL_WIDTH, SUIT_H_ALL(219));
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 2) {

    }
    return UIEdgeInsetsZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeZero;
}

#pragma mark <UIScrollViewDelegate>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

}

@end
